package top.lishuoboy.knife4j.swagger3_0_0.demo.controller;

import cn.hutool.core.collection.ListUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lishuoboy.knife4j.swagger3_0_0.demo.dto.*;
import top.lishuoboy.knife4j.swagger3_0_0.entry.User;

@Api(tags = "用户管理接口")
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {
    /** 查询用户列表 */
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 10)
    @PostMapping("getAll")   // get请求有问题，knife4j浏览器页面无法发送请求体，因为浏览器get不支持传请求体
    public RespUsers getAll(@RequestBody ReqHead reqHead) {
        log.warn("{}", reqHead);
        return RespUsers.builder()
            .txId(reqHead.getTxId())
            .code(0)
            .msg("成功")
            .data(ListUtil
                .list(false,
                    User.builder().id(1).userName("张三").build(),
                    User.builder().id(2).userName("李四").build())
            ).build();
    }

    /** 查询用户 */
    @ApiOperation(value = "查询用户")
    @ApiOperationSupport(order = 11)
    @PostMapping("getById")
    public RespUser getById(@RequestBody ReqUser reqUser) {
        log.warn("{}", reqUser);
        return RespUser.builder()
            .txId(reqUser.getTxId())
            .code(0)
            .msg("成功")
            .data(User.builder().id(1).userName("张三").build())
            .build();
    }

    /** 删除用户 */
    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 20)
    @PostMapping("deleteById")
    public RespHead deleteById(@RequestBody ReqUser reqUser) {
        log.warn("{}", reqUser);
        return RespHead.builder()
            .txId(reqUser.getTxId())
            .code(0)
            .msg("成功")
            .build();
    }

    /** 创建用户 */
    @ApiOperation("创建用户")
    @ApiOperationSupport(order = 30)
    @PostMapping("create")
    public RespUser create(@RequestBody ReqUser reqUser) {
        log.warn("{}", reqUser);
        return RespUser.builder()
            .txId(reqUser.getTxId())
            .code(0)
            .msg("成功")
            .data(User.builder().id(1).userName("张三").build())
            .build();
    }

    /** 更新用户 */
    @ApiOperation("更新用户")
    @ApiOperationSupport(order = 40)
    @PostMapping("updateById")
    public RespHead updateById(@RequestBody ReqUser reqUser) {
        return RespHead.builder()
            .txId(reqUser.getTxId())
            .code(0)
            .msg("成功")
            .build();
    }
}